Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre commit Hooks (black, flake8, mypy, etc) [CT-105] #4639

Merged
merged 34 commits into from
Feb 11, 2022
Merged

Conversation

iknox-fa
Copy link
Contributor

@iknox-fa iknox-fa commented Jan 27, 2022

resolves #3195 Jira CT-105

Description

Adds a selection of pre-commit hooks, most importantly the black formatter.
Moves non-environment dependent tests to pre-commit and out of tox (mypy and flake8)
To enable the pre-commit hooks simply make dev.

Checklist

  • I have signed the CLA
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR

@iknox-fa iknox-fa requested a review from a team as a code owner January 27, 2022 22:39
@cla-bot cla-bot bot added the cla:yes label Jan 27, 2022
@iknox-fa iknox-fa changed the title Pre commit [CT-105] Pre commit Hooks (black) [CT-105] Jan 27, 2022
@leahwicz
Copy link
Contributor

@iknox-fa have you tried running this yet to see what we need to fix up in our code base? I'm guessing we don't abide by this perfectly right now 😄

@leahwicz
Copy link
Contributor

Also we should add something to the Contributing file so contributors know to expect this to fail during commits if they have issues

@gshank
Copy link
Contributor

gshank commented Jan 31, 2022

I'm guessing that this doesn't limit itself to the things that were just changes, so this should include a reformatting of the entire codebase, right?

In particular we have never event run flake8 on the test directories. I'm wondering if it might not be easier to skip the test/unit and test/integration directories for now, since they're going to change a lot soon.

@iknox-fa
Copy link
Contributor Author

iknox-fa commented Feb 1, 2022

@iknox-fa have you tried running this yet to see what we need to fix up in our code base? I'm guessing we don't abide by this perfectly right now 😄

Its... a lot. 306 of 332 files were reformatted when I did a test run locally.

@iknox-fa
Copy link
Contributor Author

iknox-fa commented Feb 1, 2022

Also we should add something to the Contributing file so contributors know to expect this to fail during commits if they have issues

Good call. Updated

@iknox-fa
Copy link
Contributor Author

iknox-fa commented Feb 1, 2022

I'm guessing that this doesn't limit itself to the things that were just changes, so this should include a reformatting of the entire codebase, right?

Be default it just corrects files that you are currently trying to commit, but you can run it against everything of course. We will want to run it against everything just to minimize the confusion that might come from a PR touching so many files, but I'd like to do it in a separate PR.

In particular we have never event run flake8 on the test directories. I'm wondering if it might not be easier to skip the test/unit and test/integration directories for now, since they're going to change a lot soon.
I think we should go ahead and do everything, in theory black makes the testing code easier to read so it would be useful to do before you go refactor a ton of it.

Copy link
Contributor

@kwigley kwigley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! Can we please enforce these pre-commit hooks in CI and move the black config (line length) to a config file (pyproject.toml) so that IDEs, editors, and running black . directly on the cli can all use the same config?

@iknox-fa
Copy link
Contributor Author

iknox-fa commented Feb 3, 2022

Love it! Can we please enforce these pre-commit hooks in CI and move the black config (line length) to a config file (pyproject.toml) so that IDEs, editors, and running black . directly on the cli can all use the same config?

pyproject.toml has further reaching implications than just linting stuff! That said I'm all for it (and it's the only way if you want to escape the hellscape of setuptools some day).

I went ahead and put this in another ticket, as it may cause issues with some of the other tools that build/interact with dbt.

@iknox-fa iknox-fa requested a review from kwigley February 4, 2022 15:23
Copy link
Contributor

@kwigley kwigley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are some references in CONTRIBUTING.md to tox and flake8/mypy, can you update this as well?

https://github.com/dbt-labs/dbt-core/blob/pre-commit-CT-105/CONTRIBUTING.md#tox

tox.ini Outdated
@@ -2,25 +2,6 @@
skipsdist = True
envlist = py37,py38,py39,flake8,mypy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
envlist = py37,py38,py39,flake8,mypy
envlist = py37,py38,py39

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Updated!

- name: Run tox
run: tox
- name: Run pre-commit hooks
run: pre-commit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be something like:

Suggested change
run: pre-commit
run: pre-commit --all-files --show-diff-on-failure

There are no staged files when this workflow runs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh good catch (show-diff-on-failures doesn't seem to be a thing however). Fixed!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! and more on --show-diff-on-failure here. totally optional but I've found it helpful in CI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failure singular! whomp whomp adding it now.

Makefile Outdated
Comment on lines 32 to 35
@\
$(DOCKER_CMD) pre-commit run flake8 | grep -v "[INFO]"; \
$(DOCKER_CMD) pre-commit run mypy | grep -v "[INFO]"; \
$(DOCKER_CMD) pre-commit run black | grep -v "[INFO]" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only run on git staged files. Is that what is intended here?

@iknox-fa
Copy link
Contributor Author

iknox-fa commented Feb 8, 2022

This will only run on git staged files. Is that what is intended here?
Nope.. fixed.

@iknox-fa
Copy link
Contributor Author

I think there are some references in CONTRIBUTING.md to tox and flake8/mypy, can you update this as well?

https://github.com/dbt-labs/dbt-core/blob/pre-commit-CT-105/CONTRIBUTING.md#tox

Whoops. Missed this comment initially. Fixed now!

Copy link
Contributor

@kwigley kwigley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! I'd love if someone else from the team could take this for a spin as well 👍

Copy link
Contributor

@nathaniel-may nathaniel-may left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working to meet my linting needs on this one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CT-105] use black code formatter
6 participants